import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'my_menu',
      component: () => import( '../components/my_menu'),
      meta: {title: '导航栏'},
      children: [
        {
          path: 'query',
          name: 'query',
          component: () => import( '../views/query'),
          meta: {title: '查询'},
        }, {
          path: 'record',
          name: 'record',
          component: () => import( '../views/record'),
          meta: {title: '记录'},
        }, {
          path: 'subject',
          name: 'subject',
          component: () => import( '../views/subject'),
          meta: {title: '科目管理'},
        }, {
          path: 'log',
          name: 'log',
          component: () => import( '../views/log'),
          meta: {title: '日志'},
        }, {
          path: 'chart',
          name: 'chart',
          component: () => import( '../views/chart'),
          meta: {title: '统计'},
        }
      ]
    }, {
      path: '/login',
      name: 'login',
      component: () => import( '../views/login'),
      meta: {title: '登录'},
    }, {
      path: '/register',
      name: 'register',
      component: () => import( '../views/register'),
      meta: {title: '注册'},
    }, {
      path: '/error',
      name: 'error',
      component: () => import( '../views/error'),
      meta: {title: 'error'},
    },
  ]
})

let routers = router.getRoutes().map(item => item.name)
router.beforeEach((to, from, next) => {
  if (routers.includes(to.name)) {
    next()
  } else {
    next('/error')
  }
})
export default router
